package com.example.basiclib.application;

import android.content.Context;
import android.content.Intent;
import android.net.ParseException;
import android.support.annotation.Keep;
import android.text.TextUtils;

import com.google.gson.JsonIOException;
import com.google.gson.JsonParseException;
import com.jess.arms.utils.ArmsUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.Objects;

import me.jessyan.rxerrorhandler.handler.listener.ResponseErrorListener;
import retrofit2.HttpException;
import timber.log.Timber;

@Keep
public class ResponseErrorListenerImpl implements ResponseErrorListener {
    private static final String TAG = "ResponseErrorListenerIm";

    @Override
    public void handleResponseError(Context context, Throwable t) {
        Timber.tag(TAG).e(t);
        //这里不光是只能打印错误,还可以根据不同的错误作出不同的逻辑处理
        String msg = "";
        if (t instanceof ServiceException) {
            msg = handleServiceException((ServiceException) t, context);
        } else if (t instanceof UnknownHostException) {
            msg = "网络不可用";
        } else if (t instanceof SocketTimeoutException) {
            msg = "请求网络超时";
        } else if (t instanceof HttpException) {
            HttpException httpException = (HttpException) t;
            msg = convertStatusCode(httpException, context);
        } else if (t instanceof JsonParseException || t instanceof ParseException || t instanceof JSONException || t instanceof JsonIOException) {
            Timber.tag(getClass().getName()).e(t);
            msg = "数据解析错误";
        }
        if (!TextUtils.isEmpty(msg))
            ArmsUtils.makeText(context, msg);
    }

    private String handleServiceException(ServiceException t, Context context) {
        String msg;
        msg = t.getServiceMessage();
        int serviceCode = t.getServiceCode();
        switch (serviceCode) {
            case 8://令牌被撤销
            case 9://令牌被撤销
            case 10000: //登录超时
                startLoginActivity(context, msg);
                break;
        }
        return msg;
    }

    private void startLoginActivity(Context context, String msg) {

    }

    private String convertStatusCode(HttpException httpException, Context context) {
        String msg;
        Timber.tag(TAG).e("%s", httpException.code());
        if (httpException.code() == 500) {
            msg = "服务器发生错误";
        } else if (httpException.code() == 404) {
            msg = "请求地址不存在";
        } else if (httpException.code() == 403) {
            msg = "请求被服务器拒绝";
        } else if (httpException.code() == 307) {
            msg = "请求被重定向到其他页面";
        } else {
            try {
                final String string = Objects.requireNonNull(httpException.response().errorBody()).string();
                JSONObject jsonObject = new JSONObject(string);
                final int code = jsonObject.getInt("code");
                msg = jsonObject.getString("message");
                if (code != 0) {
                    final ServiceException serviceException = new ServiceException(msg, code);
                    handleServiceException(serviceException, context);
                }
            } catch (Exception e) {
                Timber.tag(TAG).e("service error cannot analysis ");
                msg = httpException.getMessage();
            }
        }
        return msg;
    }
}
